package com.mng;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


public class MybatisTest {
    public static SqlSessionFactory getSqlSessionFactory()  {
        try{
            return new SqlSessionFactoryBuilder()
                    .build(new FileInputStream("src/main/resources/mybatis-config.xml"));

        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
    @org.junit.Test
    public void test() {
        SqlSessionFactory sqlSessionFactory = MybatisTest.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);

//        List<String> nameList = mapper.getNameContainsN("li");
//        System.out.println(nameList);

//        Map<Long, Employee> map = mapper.getByIdReturnMap(1L);
//        System.out.println(map);

//        Employee employee = mapper.getById(1L);
//        System.out.println(employee);

        Map<String,Object> map = mapper.getMapById(1L);
        System.out.println(map);

//        Map<String,Object> map = mapper.getMapByName("liu");
//        System.out.println(map);
        sqlSession.close();
    }

    /**
     * association test
     */
    @Test
    public  void test2(){
        SqlSessionFactory sqlSessionFactory = MybatisTest.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        EmployeeMapMapper mapper = sqlSession.getMapper(EmployeeMapMapper.class);
        Employee employee = mapper.getEmpById(1L);
        System.out.println(employee);

//        Employee employee = mapper.getEDById(1L);
//        System.out.println(employee);
//        System.out.println(employee.getDepartment());
    }

    /**
     * collection test
     */
    @Test
    public  void test3(){
        SqlSessionFactory sqlSessionFactory = MybatisTest.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        DepartmentMapper mapper = sqlSession.getMapper(DepartmentMapper.class);
//        List<Department> department = mapper.getDep();
//        department.forEach(System.out::println);


        List<Department> department = mapper.getDep2();
        department.forEach(System.out::println);

    }
    /**
     * 动态sql
     */
    @Test
    public  void test4(){
        SqlSessionFactory sqlSessionFactory = MybatisTest.getSqlSessionFactory();
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        EmployeeDSQLMapper mapper = sqlSession.getMapper(EmployeeDSQLMapper.class);
        Department department = new Department();
        department.setName("dep1");
        String employeeName;
        /**
         * if
         */
//        employeeName="liu";
//        List<Employee> employeeList = mapper.getEmpByNameAndDept(employeeName,department);
//        employeeList.forEach(System.out::println);
        /**
         * choose
         */
//        Long id=1L;
//        employeeName = "hhh";
//        department.setName("dep1");
//        List<Employee> employeeList = mapper.getEmp(id,null,department);
//        employeeList.forEach(System.out::println);

        /**
         * foreach
         */
        department.setName("dep1");
        Department department2 = new Department();
        department2.setName("dep2");
        List<Department> departmentList = new ArrayList<>();
        departmentList.add(department);
        departmentList.add(department2);
        List<Employee> employeeList = mapper.getEmpByDeptList(departmentList);
        employeeList.forEach(System.out::println);
    }

}
